<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="创建人" prop="logOperateBy">
        <el-select v-model="queryParams.logOperateBy" placeholder="请选择创建人" clearable @change="handleQuery">
          <el-option v-for="user in userList" :key="user.userId" :label="user.userName" :value="user.userId" />
        </el-select>
      </el-form-item>

      <el-form-item label="库房名称" prop="logSrId">
        <el-select v-model="queryParams.logSrId" placeholder="请选择库房名称" clearable @change="handleQuery">
          <el-option v-for="sr in srList" :key="sr.srId" :label="sr.srName" :value="sr.srId" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['eamp:sr_log:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['eamp:sr_log:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sr_logList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="logId" />
      <el-table-column label="创建人" align="center" prop="logOperateBy">
        <template slot-scope="scope">
          {{getUserName(scope.row.logOperateBy)}}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="logOperateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.logOperateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作名称" align="center" prop="logMethodName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.eamp_sr_use_type" :value="scope.row.logMethodName" />
        </template>
      </el-table-column>
      <el-table-column label="库房名称" align="center" prop="logSrId">
        <template slot-scope="scope">
          {{getSrName(scope.row.logSrId)}}
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="logReId">
        <template slot-scope="scope">
          {{getReName(scope.row.logReId)}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-search" @click="handleView(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['eamp:sr_log:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 查看操作记录对话框 -->
    <el-dialog title="查看" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="创建人" prop="logOperateBy">
          <el-input :value="getUserName(form.logOperateBy)" placeholder="请输入日志创建人" disabled />
        </el-form-item>
        <el-form-item label="操作名称" prop="logMethodName">
          <el-input :value="getDictName(form.logMethodName)" type="textarea" placeholder="请输入内容" disabled />
        </el-form-item>
        <el-form-item label="库房名称" prop="logSrId">
          <el-input :value="getSrName(form.logSrId)" placeholder="请输入库房名称" disabled />
        </el-form-item>
        <el-form-item label="设备名称" prop="logReId">
          <el-input :value="getReName(form.logReId)" placeholder="请输入参试设备名称" disabled />
          <el-button type="primary" v-if="form.logReId" size="mini" @click="viewDeviceDetails(form.logReId)">查看</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="查看设备详情" :visible.sync="Device" width="500px" append-to-body>
      <el-form :model="reDetail">
        <el-form-item label="设备名称">
          <el-input v-model="reDetail.reName" disabled></el-input>
        </el-form-item>
        <el-form-item label="设备类型">
          <el-input v-model="reDetail.reModel" disabled></el-input>
        </el-form-item>
        <el-form-item label="设备状态">
          <el-input :value="getStateName(reDetail.reState)" disabled></el-input>
        </el-form-item>
        <el-form-item label="设备使用状态">
          <el-input :value="getUseStateName(reDetail.reUseState)" disabled></el-input>
        </el-form-item>
        <el-form-item label="设备备注">
          <el-input v-model="reDetail.reRemark" disabled></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="Devicecancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {listSr_log, delSr_log, getUserList, getSrList,getReList,getReDetail} from "@/api/eamp/sr_log";
import {  getDicts,  } from "@/api/system/dict/data";
export default {
  dicts: ['eamp_sr_type', 'eamp_sr_use_type'],
  name: "Sr_log",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库房操作记录表格数据
      sr_logList: [],
      srList: [],//用于缓存库房数据
      userList: [], // 用于缓存用户数据
      typeDictList:[],
      userTypeDictList:[],
      stateDictList: [],
      useDictList: [],
      // 设备列表
      reList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      Device:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        logOperateBy: null,
        logOperateTime: null,
        logMethodName: null,
        logSrId: null,
        logReId: null,
        delFlg: 1
      },
      // 表单参数
      form: {},
      reDetail: {  
        reName: '',  
        reModel: '',  
        reState: '',  
        reUseState: '',  
        reRemark: '',  
      }, 
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.getUserList();
    this.getSrList();
    this.getDictList();
    this.getReList();
  },
  methods: {
    getDictList(){
      getDicts("eamp_sr_type").then(res => {
        this.typeDictList = res.data;
        // console.log(this.typeDictList)
      });
      getDicts("eamp_sr_use_type").then(res => {
        this.userTypeDictList = res.data;
        // console.log(this.userTypeDictList)
      });
      getDicts("eamp_re_state").then((res) => {
        this.stateDictList = res.data;
        // console.log(this.stateDictList)
      });
      getDicts("eamp_re_use_state").then((res) => {
        this.useDictList = res.data;
        // console.log(this.useDictList)
      });
    },
    getDictName(logMethodName){
      const dict = this.userTypeDictList.find(dict => dict.dictValue === logMethodName);
      return dict ? dict.dictLabel : '未知';
    },
    getStateName(reState){
      const state = this.stateDictList.find(state => state.dictValue == reState);
      return state ? state.dictLabel : '未知';
    },
    getUseStateName(reUseState){
      const useState = this.useDictList.find(useState => useState.dictValue == reUseState);
      return useState ? useState.dictLabel : '未知';
    },
    /** 查询库房操作记录列表 */
    getList() {
      listSr_log(this.queryParams).then(response => {
        this.sr_logList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    getUserList() {
      getUserList().then(response => {
        this.userList = response.data; // 缓存用户数据
        // console.log(this.userList)
      })
    },
    getUserName(logOperateBy){
      const user = this.userList.find(user => user.userId === logOperateBy);
      return user ? user.userName : '未知';
    },
    getSrList(){
      getSrList().then(response => {
        this.srList = response.data; // 缓存库房数据
        // console.log(this.srList)
      });
    },
    getSrName(logSrId){
      const sr = this.srList.find(user => user.srId === logSrId);
      return sr ? sr.srName : '未知';
    },
    getReList(){
      getReList().then(response => {
        this.reList = response.data;
        // console.log(this.reList);
        
      })
    },
    getReName(logReId){
      const re = this.reList.find(user => user.reId === logReId);
      return re ? re.reName : '无';
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        logId: null,
        logOperateBy: null,
        logOperateTime: null,
        logMethodName: null,
        logSrId: null,
        logReId: null,
        delFlg: 1
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.logId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleView(row){
      this.form = row;
      this.open = true;
    },
    /** 查看设备详情*/
    viewDeviceDetails(logRecord){
      // console.log(logRecord);
      getReDetail(logRecord).then(res => {
        // console.log(res);
        this.reDetail = res.data;
      })
      this.Device=true;
    }, 
    Devicecancel(){
      this.Device=false;
      this.resetForm(); // 重置表单数据 
    },
    resetForm() {  
      this.reDetail = {  
        reName: '',  
        reModel: '',  
        reState: '',  
        reUseState: '',  
        reRemark: '',  
      };  
    }, 
    /** 删除按钮操作 */
    handleDelete(row) {
      const logIds = row.logId || this.ids;
      this.$modal.confirm('是否确认删除库房操作记录编号为"' + logIds + '"的数据项？').then(function () {
        return delSr_log(logIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('eamp/sr_log/export', {
        ...this.queryParams
      }, `sr_log_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
